package cn.itmantzy.pet.service.impl;


import cn.itmantzy.basic.service.impl.BaseServiceImpl;
import cn.itmantzy.basic.util.*;
import cn.itmantzy.order.domain.PetAcquisitionOrder;
import cn.itmantzy.order.mapper.PetAcquisitionOrderMapper;
import cn.itmantzy.order.service.IPetAcquisitionOrderService;
import cn.itmantzy.org.domain.Employee;
import cn.itmantzy.org.domain.Shop;
import cn.itmantzy.org.mapper.EmployeeMapper;
import cn.itmantzy.org.mapper.ShopMapper;
import cn.itmantzy.pet.domain.Pet;
import cn.itmantzy.pet.domain.PetDetail;
import cn.itmantzy.pet.domain.SearchMasterMsg;
import cn.itmantzy.pet.mapper.PetDetailMapper;
import cn.itmantzy.pet.mapper.SearchMasterMsgMapper;
import cn.itmantzy.pet.query.SearchMasterMsgQuery;
import cn.itmantzy.pet.service.IPetService;
import cn.itmantzy.pet.service.ISearchMasterMsgService;
import cn.itmantzy.user.domain.Logininfo;
import cn.itmantzy.user.domain.User;
import cn.itmantzy.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper masterMsgMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPetService petService;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg masterMsg, HttpServletRequest request) {
        // 1.获取登录用户信息，拿到user_id(通过token获取用户登录信息，由于后面也需要登录用户信息，所以可以抽取工具类)
        Logininfo currentLogininfo = LoginContext.getCurrentLogininfo(request);
        // 获取user_id
        User user = userMapper.loadByLogininfoId(currentLogininfo.getId());
        // 将user_id存入寻主信息对象
        masterMsg.setUser_id(user.getId());
        // 2.查询最近店铺信息，拿到shop_id
        // 获取到宠物主人所在位置的经纬度
        Point point = DistanceUtil.getPoint(masterMsg.getAddress());
        // 获取到最近的店铺信息
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        // 存入店铺id
        masterMsg.setShop_id(nearestShop.getId());
        // 3.保存寻主信息
        masterMsgMapper.save(masterMsg);
    }

    /**
     * 待处理寻主消息业务实现
     * @param query
     * @param logininfo
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo logininfo) {
        // 待处理寻主消息
        query.setState(0);
        // 首先根据logininfo_id找到登录员工信息
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        // 判断shop_id是否为空，如果不为空，给客户端传递的条件中的shop_id赋值
        if(employee.getShop_id() != null){
            // 店铺员工查询的待处理消息
            query.setShop_id(employee.getShop_id());
        }
        return super.queryPage(query);
    }


    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo logininfo) {
        // 已处理寻主消息
        query.setState(1);
        // 查询出员工信息，判断该员工属于平台员工还是店铺员工，店铺员工只能查询店铺的寻主信息
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id() != null){
            query.setShop_id(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, Logininfo logininfo) {
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        if(user != null){
            query.setUser_id(user.getId());
        }
        return super.queryPage(query);
    }

    /**
     * 处理待处理消息业务
     * @param pet
     * @param logininfo
     */
    @Override
    @Transactional
    public void handle(Pet pet, Logininfo logininfo) {
        // 1.修改寻主信息状态为已处理
        masterMsgMapper.state2Processed(pet.getSearch_master_msg_id());
        // 2.保存宠物信息 + 宠物细节
        petService.add(pet); // 返回自增id，因为还需要保存宠物细节
        // @TODO 3.打开订单页面
        SearchMasterMsg masterMsg = masterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        PetAcquisitionOrder acquisitionOrder = pet2order(pet, masterMsg, employee.getId());
        petAcquisitionOrderMapper.save(acquisitionOrder);
        // @TODO 4.完成支付

    }


    // 订单
    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt, Long employeeId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对" + pet.getName() + "收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(employeeId);
        return order;
    }
}
